package others.swing.p012;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

class Manager extends Frame implements ActionListener, ItemListener {
    int i;
    Box baseBox, box1, box2, box3;
    CheckboxGroup sex;
    Checkbox mbox, wbox;  //定义两个选择框
    Panel panel1, panel2;  //定义两个面板
    Label introduce, numLabel, nameLabel, classLabel, sexLabel, roomLabel, phoneLabel, showLabel, emptyLabel;// 定义9个标签
    TextField numTextField, nameTextField, classTextField, roomTextField, phoneTextField;//定义5个标签
    Button insert, update, inquire, delete, clearButton, exitButton; //定义6个按钮
    TextArea text;  //定义1个文本域

    Manager() {
        super("管理员操作界面！");//设置窗体标题
        this.setFont(new Font("隶书", Font.BOLD, 18));//设置界面的字体
        this.setBackground(Color.pink);//设置界面背景颜色
        sex = new CheckboxGroup();
        introduce = new Label("欢迎进入管理员操作界面！");
        panel1 = new Panel();//初始化面板
        panel2 = new Panel();
        //初始化两个标签，用于提示字符信息
        numLabel = new Label("   学     号:");
        nameLabel = new Label("   姓     名:");
        classLabel = new Label("   班     级:");
        sexLabel = new Label("   性     别:");
        roomLabel = new Label("   宿     舍:");
        phoneLabel = new Label("   电     话:");
        showLabel = new Label("操作后信息：");
        emptyLabel = new Label();
        //初始化两个文本框的显示宽度
        numTextField = new TextField(12);
        nameTextField = new TextField(12);
        classTextField = new TextField(12);
        roomTextField = new TextField(12);
        phoneTextField = new TextField(12);
        //初始化选择框
        mbox = new Checkbox("男", false, sex);
        wbox = new Checkbox("女", false, sex);
        //初始化按钮
        insert = new Button("录入");
        update = new Button("更新");
        inquire = new Button("显示所有信息");
        delete = new Button("删除");
        clearButton = new Button("清空");
        exitButton = new Button("退出");
        //初始化文本区
        text = new TextArea(6, 45);
        text.setEditable(false);

        panel1.add(introduce);// 将界面说明的标签加入面板
        //将各个标签，按钮，按照需要加入box1中
        box1 = Box.createVerticalBox();
        box1.add(numLabel);
        box1.add(Box.createVerticalStrut(8));
        box1.add(nameLabel);
        box1.add(Box.createVerticalStrut(8));
        box1.add(classLabel);
        box1.add(Box.createVerticalStrut(8));
        box1.add(sexLabel);
        box1.add(Box.createVerticalStrut(8));
        box1.add(roomLabel);
        box1.add(Box.createVerticalStrut(8));
        box1.add(phoneLabel);
        box1.add(Box.createVerticalStrut(8));
        box1.add(showLabel);
        //将各个标签，按钮，按照需要加入box2中
        panel2.add(mbox);
        panel2.add(Box.createHorizontalStrut(10));
        panel2.add(wbox);
        box2 = Box.createVerticalBox();
        box2.add(numTextField);
        box2.add(Box.createVerticalStrut(8));
        box2.add(nameTextField);
        box2.add(Box.createVerticalStrut(8));
        box2.add(classTextField);
        box2.add(Box.createVerticalStrut(8));
        box2.add(panel2);
        box2.add(Box.createVerticalStrut(8));
        box2.add(roomTextField);
        box2.add(Box.createVerticalStrut(8));
        box2.add(phoneTextField);
        box2.add(Box.createVerticalStrut(8));
        box2.add(emptyLabel);
        //将各个标签，按钮，按照需要加入box3中
        box3 = Box.createVerticalBox();
        box3.add(insert);
        box3.add(Box.createVerticalStrut(20));
        box3.add(inquire);
        box3.add(Box.createVerticalStrut(20));
        box3.add(update);
        box3.add(Box.createVerticalStrut(20));
        box3.add(delete);
        box3.add(Box.createVerticalStrut(20));
        box3.add(clearButton);
        box3.add(Box.createVerticalStrut(20));
        box3.add(exitButton);
        //将box1,box2,box3加入baseBox中
        baseBox = Box.createHorizontalBox();
        baseBox.add(box1);
        baseBox.add(Box.createHorizontalStrut(10));
        baseBox.add(box2);
        baseBox.add(Box.createHorizontalStrut(40));
        baseBox.add(box3);

        setLayout(new FlowLayout());//设置窗体上的各控件的布局为流式布局
        add(panel1);//将面板加入界面
        add(baseBox);//将baseBox加入界面中
        add(text);
        //为选择框注册监听器对象
        mbox.addItemListener(this);
        wbox.addItemListener(this);
        //为输入文本框注册监听器对象
        numTextField.addActionListener(this);
        nameTextField.addActionListener(this);
        classTextField.addActionListener(this);
        roomTextField.addActionListener(this);
        phoneTextField.addActionListener(this);
        //为按钮注册监听器对象
        insert.addActionListener(this);
        update.addActionListener(this);
        inquire.addActionListener(this);
        delete.addActionListener(this);
        clearButton.addActionListener(this);
        exitButton.addActionListener(this);
        //采用匿名类实现窗体标题栏上“×”按钮的关闭功能
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent ew) {
                dispose();   //关闭窗口并释放所占屏幕资源
            }
        });
        setBounds(100, 100, 500, 550);//设置窗体的起始坐标，以及宽度和高度
        setVisible(true);//让窗体可见
        setResizable(false);//让窗体不可修改

    }

    @Override
    //学生性别控件响应
    public void itemStateChanged(ItemEvent ei) {
        if (mbox.getState()) {
            i = 1;
        } else if (wbox.getState()) {
            i = 0;
        }

    }

    @Override
    public void actionPerformed(ActionEvent ea) {
        //连接数据库
        Connection con;
        Statement sql, sql1;
        ResultSet rs, rs1;
        String sextype = "";
        //选择学生性别
        if (i == 1) {
            sextype = "男";
        } else if (i == 0) {
            sextype = "女";
        }
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            con = DriverManager.getConnection("jdbc:odbc:dormitory2", "", "");
            sql = con.createStatement();
            sql1 = con.createStatement();
            String num, name, class1, sex, room, phone, recode, recode1, insertStr, updateStr, insertStr1, delStr, delStr1;
            //初始化各个所需字符串，使他们等于输入的相关信息
            num = numTextField.getText();
            name = nameTextField.getText();
            class1 = classTextField.getText();
            sex = sextype;
            room = roomTextField.getText();
            phone = phoneTextField.getText();
            text.setForeground(Color.blue);//设置字体颜色
            //录入学生信息
            if (ea.getSource() == insert) {
                String pwd = "123", type = "学生";  //在Users表中增加相应的信息
                recode1 = "(" + "'" + name + "'" + "," + "'" + pwd + "'" + "," + "'" + type + "'" + ")";
                if ("".equals(name) || name == null) {
                    JOptionPane.showMessageDialog(this, "请检查输入信息！", "友情提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                insertStr1 = "INSERT INTO Users VALUES" + recode1;
                sql1.executeUpdate(insertStr1);

                recode = "(" + "'" + num + "'" + "," + "'" + name + "'" + "," + "'" + class1 + "'" + "," + "'" + sex + "'" + "," + "'" + room + "'" + "," + "'" + phone + "'" + ")";
                insertStr = "INSERT INTO Room VALUES" + recode;
                sql.executeUpdate(insertStr);
                rs = sql.executeQuery("SELECT * FROM Room");
                while (rs.next()) {
                    num = rs.getString(1);
                    name = rs.getString(2);
                    class1 = rs.getString(3);
                    sex = rs.getString(4);
                    room = rs.getString(5);
                    phone = rs.getString(6);
                    text.append("学号：" + num + "|姓名：" + name + "|班级：" + class1 + "|性别：" + sex + "|宿舍：" + room + "|电话：" + phone + "\n");
                }
                //更新学生信息
            } else if (ea.getSource() == update) {
                updateStr = "UPDATE Room SET name=" + "'" + name + "',"
                        + " class=" + "'" + class1 + "',"
                        + " sex=" + "'" + sex + "',"
                        + " room=" + "'" + room + "',"
                        + " phone=" + "'" + phone + "'"
                        + " WHERE number=" + "'" + num + "'or name=" + "'" + name + "'";
                sql.executeUpdate(updateStr);
                rs = sql.executeQuery("SELECT * FROM Room  WHERE room='" + room + "'");
                while (rs.next()) {
                    num = rs.getString(1);
                    name = rs.getString(2);
                    class1 = rs.getString(3);
                    sex = rs.getString(4);
                    room = rs.getString(5);
                    phone = rs.getString(6);
                    text.append("学号：" + num + "|姓名：" + name + "|班级：" + class1 + "|性别：" + sex + "|宿舍：" + room + "|电话：" + phone + "\n");
                }
                //查询学生信息
            } else if (ea.getSource() == inquire) {
                rs = sql.executeQuery("SELECT * FROM Room");
                while (rs.next()) {
                    num = rs.getString(1);
                    name = rs.getString(2);
                    class1 = rs.getString(3);
                    sex = rs.getString(4);
                    room = rs.getString(5);
                    phone = rs.getString(6);
                    text.append("学号：" + num + "|姓名：" + name + "|班级：" + class1 + "|性别：" + sex + "|宿舍：" + room + "|电话：" + phone + "\n");
                }
                //删除学生信息
            } else if (ea.getSource() == delete) {
                delStr1 = "DELETE FROM Users WHERE name='" + name + "'";    //删除Users表中相应的信息
                sql1.executeUpdate(delStr1);

                delStr = "DELETE FROM Room WHERE number='" + num + "' or name='" + name + "'";
                sql.executeUpdate(delStr);
                rs = sql.executeQuery("SELECT * FROM Room");
                while (rs.next()) {
                    num = rs.getString(1);
                    name = rs.getString(2);
                    class1 = rs.getString(3);
                    sex = rs.getString(4);
                    room = rs.getString(5);
                    phone = rs.getString(6);
                    text.append("学号：" + num + "|姓名：" + name + "|班级：" + class1 + "|性别：" + sex + "|宿舍：" + room + "|电话：" + phone + "\n");
                }
            }
            //清空各个控件
            else if (ea.getSource() == clearButton) {
                numTextField.setText(null);
                nameTextField.setText(null);
                classTextField.setText(null);
                roomTextField.setText(null);
                phoneTextField.setText(null);
                text.setText(null);
                //退出
            } else if (ea.getSource() == exitButton) {
                dispose();
            }
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
